import md5 from 'md5'
import storage from 'store'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { Modal } from 'ant-design-vue'
import moment from 'moment'
import { mapState } from 'vuex'
const baseMixin = {
  data () {
    return {
      winHeight: 0,
      API_URL: process.env.VUE_APP_API_URL,
      MINIO_URL: process.env.VUE_APP_MINIO_URL
    }
  },
  mounted () {
    this.winHeight = document.documentElement.clientHeight || document.body.clientHeight
    window.addEventListener('resize', () => {
      this.winHeight = document.documentElement.clientHeight || document.body.clientHeight
    })
  },
  computed: {
    ...mapState({
      currentUser: (state) => state.user
    })
  },
  filters: {
    fromNow (value) {
      return moment(value).fromNow()
    }
  },
  methods: {
    isProd () {
      return process.env.NODE_ENV === 'production'
    },
    isDev () {
      return process.env.NODE_ENV === 'development'
    },
    resize () {
      window.dispatchEvent(new Event('resize'))
    },
    phone2Safety (value) {
      if (!value || value.length === 0) {
        return ''
      }
      return value.substring(0, 3) + '*****' + value.substring(8)
    },
    name2Safety (value) {
      if (!value || value.length === 0) {
        return ''
      }
      if (value.length <= 3) {
        return '*' + value.substring(1)
      } else if (value.length >= 4 && value.length <= 6) {
        return '**' + value.substring(2)
      } else if (value.length > 6) {
        return value.substring(0, 2) + '***' + value.substring(5)
      }
    },
    randomFromArr (arr) {
      if (arr instanceof Array) {
        const length = arr.length
        return arr[Math.floor(Math.random() * length)]
      }
      return null
    },
    toHome () {
      const app = this.$route.path.split('/')[1]
      this.$router.push({ path: `/${app}` })
    },
    confirm (callback, title) {
      Modal.confirm({
        title: title || '确定要删除所选择的记录么?',
        onOk () {
          callback()
        },
        onCancel () {}
      })
    },
    findConfig (code, callback) {
      this.$http.get('/zg-system/un/config/one', { name: code }, data => {
        callback(data)
      })
    },
    zeroFix (value, len) {
      return (Array(len).join(0) + parseInt(value)).slice(-len)
    },
    genSafeParams () {
      const appId = '1'
      const appSecret = 'xxx'
      const random = this.genRandomStr(6)
      const timestamp = moment().valueOf() - 200000
      const sign = this.genMd5(appSecret + random + timestamp)
      return {
        appId: appId,
        appSecret: appSecret,
        random: random,
        timestamp: timestamp,
        sign: sign
      }
    },
    genRandomStr (n) {
      const chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

      let result = ''
      for (let i = 0; i < n; i++) {
        const index = Math.ceil(Math.random() * 35)
        result += chars[index]
      }
      return result
    },
    genMd5 (str) {
      return md5(str)
    },
    genPwd (pwd) {
      return md5(pwd + 'qaz@Pwd2020')
    },
    genDefaultPwd () {
      return 'P@ssw0rd2022'
    },
    toPage (url) {
      this.$router.push(url)
    },
    toViewFile (path) {
      window.open(this.MINIO_URL + path)
    },
    genHtml (str) {
      if (!str || str.length === 0) { return '' }
      const regex = new RegExp('<img', 'gi')
      return str.replace(regex, `<img style="max-width: 100%;"`)
    },
    needLogin (callback) {
      if (storage.get(ACCESS_TOKEN)) {
        callback()
      } else {
        Modal.info({ title: '请您先登录系统' })
      }
    }
  }
}

export {
  baseMixin
}
